Dafny Examples

Method: abs


method abs(x: int) returns (y: int)

ensures true

{

if x < 0 {

y := -x;

} else {

y := x;

}

}

  1. method abs(x: int) returns (y: int):
  1. ensures true:
  1. if x < 0 {:
  1. y := -x;:
  1. else {:
  1. y := x;:

Method: foo


method foo(x: int)

requires x >= 0

{

var y := abs(x);

// assert( y == x);

}

  1. method foo(x: int):
  1. requires x >= 0:
  1. var y := abs(x);:
  1. // assert( y == x);:

Method: max


method max(x: int, y: int) returns (m: int)

requires true;

ensures true;

{

var r : int;

if x > y {

r := 0;

} else {

r := 1;

}

m := r;

// return r;

// return m;

}

  1. method max(x: int, y: int) returns (m: int):
  1. requires true;:
  1. ensures true;:
  1. var r : int;:
  1. if x > y {:
  1. r := 0;:
  1. else { r := 1; }:
  1. m := r;:

Method: ex1


method ex1(n: int)

requires true

ensures true

{

var i := 0;

while i < n

invariant true

// decreases *

{

i := i + 1;

}

// assert i == n;

}

  1. method ex1(n: int):
  1. requires true:
  1. ensures true:
  1. var i := 0;:
  1. while i < n:
  1. invariant true:
  1. // decreases *:
  1. i := i + 1;:
  1. // assert i == n;:

Method: foo2


method foo2()

ensures false

decreases *

{

while true

decreases *

{

}

assert false;

}

  1. method foo2():
  1. ensures false:
  1. decreases *:
  1. while true:
  1. assert false;:

Method: find


method find(a: seq<int>, key: int) returns (index: int)

requires true

ensures true

{

index := 0;

while index < |a|

invariant true

{

if a[index] == key {

return 0;

}

index := index + 2;

}

index := -10;

}

  1. method find(a: seq<int>, key: int) returns (index: int):
  1. requires true:
  1. ensures true:
  1. index := 0;:
  1. while index < |a|:
  1. invariant true:
  1. if a[index] == key { return 0; }:
  1. index := index + 2;:
  1. index := -10;:

Method: isPalindrome


method isPalindrome(a: seq<char>) returns (b

  

: bool)

{

return true;

}

  1. method isPalindrome(a: seq<char>) returns (b: bool):
  1. return true;:

Predicate: sorted


predicate sorted(a: seq<int>)

{

forall j, k::0 <= j < k < |a| ==> a[j] <= a[k]

}

  1. predicate sorted(a: seq<int>):
  1. forall j, k::0 <= j < k < |a| ==> a[j] <= a[k]:

Method: unique


method unique(a: seq<int>) returns (b: seq<int>)

requires sorted(a)

ensures true

{

return a;

}

  1. method unique(a: seq<int>) returns (b: seq<int>):
  1. requires sorted(a):
  1. ensures true:
  1. return a;:

Method: Main


method Main() {

var r := find([], 1);

print r, "\n";

  

r := find([0,3,5,7], 5);

print r, "\n";

  

var s1 := ['a'];

var r1 := isPalindrome(s1);

print "is [", s1, "]", " a isPalindrome? ", r1, " \n";

  

s1 := [];

r1 := isPalindrome(s1);

print "is [", s1, "]", " a isPalindrome? ", r1, " \n";

  

s1 := ['a', 'b'];

r1 := isPalindrome(s1);

print "is [", s1, "]", " a isPalindrome? ", r1, " \n";

  

s1 := ['a', 'b', 'a'];

r1 := isPalindrome(s1);

print "is [", s1, "]", " a isPalindrome? ", r1, " \n";

  

var i := [0,1,3,3,5,5,7];

var s := unique(i);

print "unique applied to ", i, " is ", s, "\n";

}

  1. method Main() {:
  1. var r := find([], 1);:
  1. print r, "\n";:
  1. Repeated Calls to isPalindrome:
  1. var i := [0,1,3,3,5,5,7];:
  1. var s := unique(i);:
  1. print "unique applied to ", i, " is ", s, "\n";:

Summary of Terms